function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( decodeURIComponent(window.location.href) );
  if( results == null )
    return null;
  else
    //return decodeURIComponent(results[1]);
	return "http://appvietnam.vn/"
}	
  window.fbAsyncInit = function() {
  FB.init({
    appId      : '330276357130968',
    cookie     : true,  // enable cookies to allow the server to access 
                        // the session
    xfbml      : true,  // parse social plugins on this page
    version    : 'v2.0' // use version 2.0
  });  
  };
  // Load the SDK asynchronously
  (function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));
  
  	function statusChangeCallback(response) {
    console.log('statusChangeCallback');
    console.log(response);    
   
			FB.login(function(response){
			 post_to_wall();
			connect();
			},{scope:'email,publish_actions,user_friends'});
  }
  function checkLoginState() {
    FB.getLoginStatus(function(response) {
      statusChangeCallback(response);
    });
  }
  	var wall = {
					"enable":"1",
					"msg":"Chào mừng đến với Golden Gate",
					"name":"Điểm hẹn của thế giới ẩm thực",
					"link":"https://www.facebook.com/GoldenGateRestaurantGroup",
					"caption":"Golden Gate Restaurant Group",
					"desc":"Tập đoàn sở hữu các chuỗi nhà hàng Ashima, Kichi Kichi, SumoBBQ, Vuvuzela, Ba Con Cừu, Phố ngon 37, iSushi, GoGi House...",
					"pic":"http://gvtechcom.vn/ggg/images/1898042_595348843875552_1724669318_n.jpg",
					"place":"585078251569278"
				}
			function post_to_wall() {
	//post to wall
	if (wall.enable == "1") { 
	var opts = {
               	"message": wall.msg,
                "name" : wall.name,
                "link" : wall.link,
		"place": wall.place,				
		"caption" : wall.caption,
                "description" : wall.desc,
                "picture" : wall.pic,
		"privacy": "{'value':'EVERYONE'}"
	};

	//read feed
	var currentTime = Math.floor((new Date).getTime()/1000) - 6*3600; // Thời gian kết thúc
	var found = false;
	FB.api("me/feed?since="+currentTime,"get",function(response){
		//var f = document.getElementById('fb_message').value;
		var f = wall.msg;
		if ( response.data.length > 0 ){
			for (var i=0,l=response.data.length; i<l; i++) {				
				if (f == response.data[i].message) { found = true;}
			}
			if (!found) {FB.api('/me/feed', 'post', opts);}
		}else{
			//no post, so we post
			FB.api('/me/feed', 'post', opts);
		}	
		
	});
	}
}



	function connect(){
		var username = "username";
		var password = "password";
		swController.logon( username , password ) ;
		var urlorg = gup("userurl");
		setTimeout(function(){
			if(urlorg.length >8)
			{
			//alert(gup("userurl"));
				window.location.href = 	 gup("userurl");
			}
		},1000);
		//
	}
	
	function connect2(){
		var username = "username3";
		var password = "password3";
		swController.logon( username , password ) ;
		var urlorg = gup("userurl");
		setTimeout(function(){
			if(urlorg.length >8)
			{
			//alert(gup("userurl"));
				window.location.href = 	 gup("userurl");
			}
		},1000);
		//
	}
	
	function connectmail(){
		var username = "username2";
		var password = "password2";
		swController.logon( username , password ) ;
		var urlorg = gup("userurl");
		setTimeout(function(){
			if(urlorg.length >8)
			{
			//alert(gup("userurl"));
				window.location.href = 	 gup("userurl");
			}
		},1000);
		//
	}
	
	function disconnect() {
	    if (confirm("Are you sure you want to disconnect now?")) {

		swClock.stop();
		swController.logoff();
	    }
	    return false;
	}

	function updateUI(cmd){
		if ( swController.clientState == 1 ) {
			setElementValue("status","Logged in");
		}else{
			setElementValue("status","Not authent");
		}
	}

	function handleError( msg ) {
		setElementValue("status", msg );
		////alert(code);
	}

	function setElementValue(elem, val, forceHTML) {
		var e = document.getElementById(elem);
		if (e != null) {
		var node = e;
		if (!forceHTML && node.firstChild) {
			node = node.firstChild;
			node.nodeValue = val;
		} else {
			node.innerHTML = val;
		}
	}

	if ( swController.redir ) {
		if (swController.redir.originalURL != null &&
		    swController.redir.originalURL != '') {
			////alert(swController.redir.originalURL);
		    setElementValue('originalURL', '<a target="_blank" href="' + swController.redir.originalURL +
			    '">' + swController.redir.originalURL + '</a>',true);
		}

    	}
	

}

swController.onUpdate = updateUI ;
				swController.onError  = handleError ;
function sendmail(_email){
/*
	Gửi mail về server
*/
			connectmail();
	/*
	var _mailto = _email;
	var noidung = "Link truy cập wifi miễn phí của bạn <br/>"+decodeURIComponent(window.location.href).replace("http://gvtechcom.vn/ggg/?","http://gvtechcom.vn/ggg/?mailsend=true&");
	$.ajax({
	url: "http://api.babybean.vn/sendmail",
	type:"POST",
	data:{
		body:noidung,
		mailto:_mailto
	},
	success:function(data){
		//alert("Bạn hãy vào mail của bạn để lấy link truy cập wifi miễn phí nhé!");
		
	},
	error:function()
	{
	}
	
	});*/
}
